#include<stdio.h>
#include<string.h>
#define maxsize 1000
#define maxlen 100

typedef struct{
	char data[maxsize];
	int top;
}stack;

void initstack(stack *s)
{
	s->top=-1;
}
int isempty(stack *s)
{
	return s->top==-1;
}
void push(stack *s,char c)
{
	s->data[++(s->top)]=c;
}
char pop(stack *s)
{
	return s->data[(s->top)--];
}
char peek(stack *s)
{
	return s->data[s->top];
}
int match(char opening,char closing)
{
	if(opening=='('&&closing==')')
		return 1;
	if(opening=='['&&closing==']')
		return 1;
	if(opening=='{'&&closing=='}')
		return 1;
	return 0;
}
int isbalanced(char *e)
{
	stack s;
	initstack(&s);
	int len=strlen(e);
	for(int i=0;i<len;i++)
	{
		if(e[i]=='('||e[i]=='['||e[i]=='{')
		{
			push(&s,e[i]);
		}
		else if(e[i]==')'||e[i]==']'||e[i]=='}')
		{
			if(isempty(&s))
			{
				return 0;
			}
			char top=peek(&s);
			if(match(top,e[i]))
			{
				pop(&s);
			}
			else
			{
				return 0;
			}
		}
	}
	return isempty(&s);
}
int main()
{
	int n;
	scanf("%d",&n);
	getchar();
	for(int i=0;i<n;i++)
	{
		char e[maxlen];
		fgets(e,maxlen,stdin);
		e[strcspn(e,"\n")]=0;
		if(isbalanced(e))
		{
			printf("Yes\n");
		}
		else
		{
			printf("No\n");
		}
	}
	return 0;
}
